package com.system.outpatientsystem.controller;

import com.system.outpatientsystem.DTO.AppointmentDTO;
import com.system.outpatientsystem.DTO.RegistrationDTO;
import com.system.outpatientsystem.pojo.Result;
import com.system.outpatientsystem.pojo.User;
import com.system.outpatientsystem.service.RegistrationService;
import com.system.outpatientsystem.service.UserService;
import org.apache.ibatis.jdbc.Null;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/registration")
public class RegistrationController {

    @Autowired
    private RegistrationService registrationService;


    @Autowired
    private UserService userService;

    @PostMapping("/createRegistration")
    public Result createRegistration(@RequestBody Map<String,Integer> params){
        Integer doctorId=params.get("doctorId");
        Integer userId=params.get("userId");
        registrationService.createRegistration(doctorId,userId);
        return Result.success("挂号成功");

    }
    @PutMapping("/getPosition")
    public Result<Integer> getPosition(@RequestBody Map<String,Integer> params){
        Integer doctorId=params.get("doctorId");
        Integer userId=params.get("userId");
        Integer position=registrationService.getPosition(doctorId,userId);
        if(position==-1) return Result.error("队列为空");
        return Result.success(position);
    }


    @PutMapping("/defaultLeaveQueue")
    public Result<User> defaultLeaveQueue(@RequestBody Map<String,Integer> params) {

        Integer doctorId=params.get("doctorId");
        Integer userId=registrationService.defaultLeaveQueue(doctorId);
        System.out.println(userId);
        User u=userService.findById(userId);
        if(u== null){
            return Result.error("队列为空！");
        }
        System.out.println(u.getName());
        return Result.success(u);
    }

    @PutMapping("/findByUserId")
    public Result<List<RegistrationDTO>> findByUserId(@RequestBody Map<String,Integer> params){
//        Integer userId=user.getId();
        Integer userId=params.get("userId");
        List<RegistrationDTO> as=registrationService.list(userId);
        return Result.success(as);
    }

}
